package com.example.myapplication3.video;

import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;

import com.example.myapplication3.video.bean.VideoItem;
import com.example.myapplication3.utils.LogUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class VideoQueryHelper {
    private static final String TAG = "VideoQueryHelper";
    private static final ExecutorService executor = Executors.newFixedThreadPool(2);

    public static void asynLoadAllVideos(Context context, int pageIndex, int itemCount, VideoLoadCallback callback) {
        executor.execute(() -> {
            List<VideoItem> videos = new ArrayList<>();

            String[] projection = {
                    MediaStore.Video.Media._ID,
                    MediaStore.Video.Media.DISPLAY_NAME,
                    MediaStore.Video.Media.DATA,
                    MediaStore.Video.Media.SIZE,
                    MediaStore.Video.Media.DURATION,
                    MediaStore.Video.Media.DATE_ADDED,
                    MediaStore.Video.Media.DATE_MODIFIED,
                    MediaStore.Video.Media.MIME_TYPE,
                    MediaStore.Video.Media.WIDTH,
                    MediaStore.Video.Media.HEIGHT
            };

            String sortOrder = MediaStore.Video.Media.DATE_ADDED + " DESC";
            Uri mediaUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;

            try (Cursor cursor = context.getContentResolver().query(
                    mediaUri,
                    projection,
                    null,
                    null,
                    sortOrder
            )) {
                if (cursor != null) {
                    cursor.moveToPosition(pageIndex * itemCount);
                    while (cursor.moveToNext()) {
                        long id = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Video.Media._ID));
                        String name = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DISPLAY_NAME));
                        String path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA));
                        long size = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.SIZE));
                        long duration = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DURATION));
                        long dateAdded = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATE_ADDED));
                        long dateModified = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATE_MODIFIED));
                        String mime = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.MIME_TYPE));
                        int width = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.WIDTH));
                        int height = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.HEIGHT));

                        Uri uri = ContentUris.withAppendedId(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, id);

                        VideoItem item = new VideoItem(id, name, path, uri, size, duration,
                                dateAdded, dateModified, mime, width, height);
                        videos.add(item);

                        if (videos.size() >= itemCount) break;
                    }
                }
            } catch (Exception e) {
                LogUtils.e(TAG, "获取视频失败", e);
                callback.onVideosLoaded(null);
                return;
            }

            callback.onVideosLoaded(videos);
        });
    }

    public interface VideoLoadCallback {
        void onVideosLoaded(List<VideoItem> videos);
    }
}